package com.chao.common.codeGeneration.source;

import com.chao.common.codeGeneration.bean.Table;
import com.chao.common.codeGeneration.bean.TableColumns;
import com.chao.common.codeGeneration.generation.FileUtile;
import com.chao.common.codeGeneration.generation.Generation;
import com.chao.common.util.DateUtile;
import com.chao.common.util.exception.SysException;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Arrays;
import java.util.List;

public class ToController {

    public static void toController(Table table, String path){
        File file = FileUtile.createFile(path+"\\controller\\"+table.getPojoKey()+"Controller.java");
        try(Writer out =new FileWriter(file)) {
            String servicePag = table.getService();
            List<String> fun = Arrays.asList(table.getFunction());
            StringBuilder sb = new StringBuilder();
            // package
            sb.append("package com.chao.").append(servicePag).append(".modular.").append(table.getPacName()).append(".controller;").append("\r\n\r\n");

            //import
            sb.append("import com.alibaba.fastjson.JSONObject;").append("\r\n");
            sb.append("import com.chao.common.util.log.Inter;").append("\r\n");
            sb.append("import com.chao.common.util.reaction.PageList;").append("\r\n");
            sb.append("import com.chao.").append(servicePag).append(".modular.").append(table.getPacName()).append(".bean.").append(table.getPojoKey()).append(";").append("\r\n");
            sb.append("import com.chao.").append(servicePag).append(".modular.").append(table.getPacName()).append(".service.").append(table.getPojoKey()).append("Service;").append("\r\n");
            sb.append("import org.springframework.beans.factory.annotation.Autowired;").append("\r\n");
            sb.append("import org.springframework.web.bind.annotation.*;").append("\r\n");
            sb.append("\r\n");
            sb.append("import java.util.Map;").append("\r\n");
            if(fun.contains("批量新增")){
                sb.append("import java.util.List;").append("\r\n");
            }

            StringBuilder pag = getUpCaseAndReverse(table.getPojoKey());
            if(pag.length() == 1){
                pag.append(table.getPojoKey().substring(table.getPojoKey().length()-2).toUpperCase());
            }
            if(pag.length() == 2){
                pag.append(table.getPojoKey().substring(table.getPojoKey().length()-1).toUpperCase());
            }
            //Class
            sb.append("\r\n").append("\r\n");
            sb.append("/**").append("\r\n");
            sb.append(" * ").append(table.getPojoName()).append("Controller").append("\r\n");
            sb.append(" * ").append(Generation.author).append("\r\n");
            sb.append(" * ").append(DateUtile.toYmd()).append("\r\n");
            sb.append(" */").append("\r\n");
            sb.append("@RestController").append("\r\n");
            sb.append("@RequestMapping(\"/").append(table.getUpojoKey()).append("\")").append("\r\n");
            sb.append("public class ").append(table.getPojoKey()).append("Controller {").append("\r\n");

            //service
            sb.append("\r\n");
            sb.append("\t/**").append("\r\n");
            sb.append("\t * ").append(table.getPojoName()).append("Service").append("\r\n");
            sb.append("\t */").append("\r\n");
            sb.append("\t@Autowired").append("\r\n");
            sb.append("\tprivate ").append(table.getPojoKey()).append("Service ").append(table.getUpojoKey()).append("Service;").append("\r\n");

            //函数

            //获取主键
            TableColumns col = null;
            for(TableColumns column: table.getColumns()){
                if(column.getKey()){
                    col = column;
                }
            }
            if(col == null){
                throw new SysException("数据表未设置主键!");
            }
            if(fun.contains("新增")){
                sb.append("\r\n");
                sb.append("\t/**").append("\r\n");
                sb.append("\t * 新增").append(table.getPojoName()).append("\r\n");
                sb.append("\t */").append("\r\n");
                sb.append("\t@Inter(\"").append(pag).append("_ADD_01\")").append("\r\n");
                sb.append("\t@RequestMapping(method = RequestMethod.POST, value = \"insert").append(table.getPojoKey()).append("\")").append("\r\n");
                sb.append("\tpublic void insert").append(table.getPojoKey()).append("(@RequestBody ").append(table.getPojoKey()).append(" ").append(table.getUpojoKey()).append("){").append("\r\n");
                sb.append("\t\t").append(table.getUpojoKey()).append("Service.insert").append(table.getPojoKey()).append("(").append(table.getUpojoKey()).append(");").append("\r\n");
                sb.append("\t}").append("\r\n");
            }

            if(fun.contains("批量新增")){
                sb.append("\r\n");
                sb.append("\t/**").append("\r\n");
                sb.append("\t * 批量新增").append(table.getPojoName()).append("\r\n");
                sb.append("\t */").append("\r\n");
                sb.append("\t@Inter(\"").append(pag).append("_ADD_02\")").append("\r\n");
                sb.append("\t@RequestMapping(method = RequestMethod.POST, value = \"insertAll").append(table.getPojoKey()).append("\")").append("\r\n");
                sb.append("\tpublic void insertAll").append(table.getPojoKey()).append("(@RequestBody List<").append(table.getPojoKey()).append("> ").append(table.getUpojoKey()).append("List){").append("\r\n");
                sb.append("\t\t").append(table.getUpojoKey()).append("Service.insertAll").append(table.getPojoKey()).append("(").append(table.getUpojoKey()).append("List);").append("\r\n");
                sb.append("\t}").append("\r\n");
            }

            if(fun.contains("修改")){
                sb.append("\r\n");
                sb.append("\t/**").append("\r\n");
                sb.append("\t * 修改").append(table.getPojoName()).append("\r\n");
                sb.append("\t */").append("\r\n");
                sb.append("\t@Inter(\"").append(pag).append("_EDT_01\")").append("\r\n");
                sb.append("\t@RequestMapping(method = RequestMethod.POST, value = \"update").append(table.getPojoKey()).append("\")").append("\r\n");
                sb.append("\tpublic void update").append(table.getPojoKey()).append("(@RequestBody ").append(table.getPojoKey()).append(" ").append(table.getUpojoKey()).append("){").append("\r\n");
                sb.append("\t\t").append(table.getUpojoKey()).append("Service.update").append(table.getPojoKey()).append("(").append(table.getUpojoKey()).append(");").append("\r\n");
                sb.append("\t}").append("\r\n");
            }

            if(fun.contains("逻辑删除")){
                sb.append("\r\n");
                sb.append("\t/**").append("\r\n");
                sb.append("\t * 删除").append(table.getPojoName()).append("\r\n");
                sb.append("\t */").append("\r\n");
                sb.append("\t@Inter(\"").append(pag).append("_DEL_01\")").append("\r\n");
                sb.append("\t@RequestMapping(method = RequestMethod.POST, value = \"delete").append(table.getPojoKey()).append("\")").append("\r\n");
                sb.append("\tpublic void delete").append(table.getPojoKey()).append("(@RequestBody ").append(table.getPojoKey()).append(" ").append(table.getUpojoKey()).append("){").append("\r\n");
                sb.append("\t\t").append(table.getUpojoKey()).append("Service.delete").append(table.getPojoKey()).append("(").append(table.getUpojoKey()).append(".get").append(col.getCapitalLetterName()).append("());").append("\r\n");
                sb.append("\t}").append("\r\n");
            }

            if(fun.contains("物理删除")){
                sb.append("\r\n");
                sb.append("\t/**").append("\r\n");
                sb.append("\t * 物理删除").append(table.getPojoName()).append("\r\n");
                sb.append("\t */").append("\r\n");
                sb.append("\t@Inter(\"").append(pag).append("_DEL_02\")").append("\r\n");
                sb.append("\t@RequestMapping(method = RequestMethod.POST, value = \"deleteData").append(table.getPojoKey()).append("\")").append("\r\n");
                sb.append("\tpublic void deleteData").append(table.getPojoKey()).append("(@RequestBody ").append(table.getPojoKey()).append(" ").append(table.getUpojoKey()).append("){").append("\r\n");
                sb.append("\t\t").append(table.getUpojoKey()).append("Service.deleteData").append(table.getPojoKey()).append("(").append(table.getUpojoKey()).append(".get").append(col.getCapitalLetterName()).append("());").append("\r\n");
                sb.append("\t}").append("\r\n");
            }

            if(fun.contains("查询详情")){
                sb.append("\r\n");
                sb.append("\t/**").append("\r\n");
                sb.append("\t * 查询").append(table.getPojoName()).append("详情").append("\r\n");
                sb.append("\t */").append("\r\n");
                sb.append("\t@Inter(\"").append(pag).append("_SEL_01\")").append("\r\n");
                sb.append("\t@RequestMapping(method = RequestMethod.GET, value = \"select").append(table.getPojoKey()).append("By").append(col.getCapitalLetterName()).append("\")").append("\r\n");
                sb.append("\tpublic ").append(table.getPojoKey()).append(" select").append(table.getPojoKey()).append("By").append(col.getCapitalLetterName()).append("(String ").append(col.getLowercaseName()).append("){").append("\r\n");
                sb.append("\t\treturn ").append(table.getUpojoKey()).append("Service.select").append(table.getPojoKey()).append("By").append(col.getCapitalLetterName()).append("(").append(col.getLowercaseName()).append(");").append("\r\n");
                sb.append("\t}").append("\r\n");
            }

            if(fun.contains("查询列表")){
                sb.append("\r\n");
                sb.append("\t/**").append("\r\n");
                sb.append("\t * 查询").append(table.getPojoName()).append("列表").append("\r\n");
                sb.append("\t */").append("\r\n");
                sb.append("\t@Inter(\"").append(pag).append("_SEL_02\")").append("\r\n");
                sb.append("\t@RequestMapping(method = RequestMethod.GET, value = \"select").append(table.getPojoKey()).append("ByList\")").append("\r\n");
                sb.append("\tpublic ").append("PageList<").append(table.getPojoKey()).append("> select").append(table.getPojoKey()).append("ByList(@RequestParam Map<String,Object> map){").append("\r\n");
                sb.append("\t\treturn ").append(table.getUpojoKey()).append("Service.select").append(table.getPojoKey()).append("ByList(new JSONObject(map));").append("\r\n");
                sb.append("\t}").append("\r\n");
            }

            if(fun.contains("分页查询")){
                sb.append("\r\n");
                sb.append("\t/**").append("\r\n");
                sb.append("\t * 分页查询").append(table.getPojoName()).append("列表").append("\r\n");
                sb.append("\t */").append("\r\n");
                sb.append("\t@Inter(\"").append(pag).append("_SEL_03\")").append("\r\n");
                sb.append("\t@RequestMapping(method = RequestMethod.GET, value = \"select").append(table.getPojoKey()).append("ByPage\")").append("\r\n");
                sb.append("\tpublic ").append("PageList<").append(table.getPojoKey()).append("> select").append(table.getPojoKey()).append("ByPage(@RequestParam Map<String,Object> map){").append("\r\n");
                sb.append("\t\treturn ").append(table.getUpojoKey()).append("Service.select").append(table.getPojoKey()).append("ByPage(new JSONObject(map));").append("\r\n");
                sb.append("\t}").append("\r\n");
            }
            sb.append("\r\n");
            sb.append("}");
            out.write(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 输出字符串中的大写字母
     */
    private static StringBuilder getUpCaseAndReverse(String str){
        StringBuilder buffer = new StringBuilder();
        // 转为char数组
        char[] ch = str.toCharArray();
        // 得到大写字母
        for (char c : ch) {
            if (c >= 'A' && c <= 'Z') {
                buffer.append(c);
            }
        }
        return buffer;
    }
}
